import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

typedef MessageCall = void Function(String? msgJson);

class AliPush {
  static const MethodChannel _channel =
      const MethodChannel('ali_push');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
  static void bindAccount(String account) async {
    _channel.invokeMethod('bindAccount', account);
    _channel.setMethodCallHandler(_onMethodCall);
  }

  static void unBindAccount() async {
    _channel.invokeMethod('unBindAccount');
  }
  
  static AliPushMessageListener? _messageListener;

  static set messageListener(AliPushMessageListener value) {
    _messageListener = value;
  }

  static Future<String> _onMethodCall(MethodCall call) async{
    dynamic arguments = call.arguments;
    switch(call.method) {
      case 'onNotification':
          if(_messageListener != null){
            _messageListener!.onNotification(arguments);
          }
        break;
      case 'onMessage':
        if(_messageListener != null){
          _messageListener!.onMessage(arguments);
        }
        break;
      case 'onNotificationOpened':
        if(_messageListener != null){
          _messageListener!.onNotificationOpened(arguments);
        }
        break;
      default:
        print('ali_push-platformMethod:${call.method}--argument:$arguments');
        return null;
    }
    return null;
  }
}
class AliPushMessageListener {
  AliPushMessageListener({
      this.onNoticationCall, this.onMessageCall, this.onNotificationOpenedCall});
  MessageCall? onNoticationCall;
  MessageCall? onMessageCall;
  MessageCall? onNotificationOpenedCall;
  void onNotification(String? arguments){
    if(onNoticationCall != null){
      onNoticationCall!.call(arguments);
    }
  }
  void onMessage(String? arguments){
    if(onMessageCall != null){
      onMessageCall!.call(arguments);
    }
  }
  void onNotificationOpened(String? arguments){
    if(onNotificationOpenedCall != null){
      onNotificationOpenedCall!.call(arguments);
    }
  }

}
